package com.heatup.apt.management.controller;

import com.heatup.apt.common.util.JsonUtils;
import com.heatup.apt.management.service.coach.CoachCourseAdjustFeeService;
import com.heatup.apt.management.service.coach.CoachCourseBasicFeeService;
import com.heatup.apt.management.service.coach.CoachCourseFeeService;
import com.heatup.apt.management.service.coach.CoachService;
import com.heatup.apt.management.service.course.CourseStandardService;
import com.heatup.apt.model.coach.CoachCourseAdjustFee;
import com.heatup.apt.model.coach.CoachCourseFee;
import com.heatup.apt.model.coach.CoachCourseFeeVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;

/**
 *
 * 教练课时费用 控制层
 *
 */
@Controller
@RequestMapping("system/coach")
public class CoachCourseFeeController extends BaseController {

    public static final Logger log = LoggerFactory.getLogger(CoachCourseFeeController.class);
    @Autowired
    private CoachCourseFeeService coachCourseFeeService;
    @Autowired
    private CoachCourseBasicFeeService coachCourseBasicFeeService;
    @Autowired
    private CoachCourseAdjustFeeService coachCourseAdjustFeeService;
    @Autowired
    private CourseStandardService courseStandardService;
    @Autowired
    private CoachService coachService;


    /**
     * 教练课时费列表
     *
     * @param coachId 教练 ID
     * @return 教练课时费列表页面
     */
    @RequestMapping("/coachCourseFeeList")
    public ModelAndView coachCourseFeeList(Integer coachId){
        ModelAndView modelAndView = new ModelAndView("coach/coach-course-fee-list");
        modelAndView.addObject("coach", coachService.queryCoachById(coachId));
        return modelAndView;
    }

    /**
     * 教练课时费分页
     *
     * @param coachCourseFee 教练课时费搜索条件
     *
     */
    @RequestMapping(value = "/coachCourseFeePage")
    @ResponseBody
    public void coachCourseFeePage(HttpServletResponse response, CoachCourseFeeVO coachCourseFee){

        responseSendMsg(response, JsonUtils.toJsonStrWithDateFormat(coachCourseFeeService.page(coachCourseFee)));

    }


    /**
     * 教练课时费编辑
     *
     * @param coachId 教练 ID
     * @return 教练课时费编辑页面
     */
    @RequestMapping("/coachCourseFeeEdit")
    public ModelAndView coachCourseFeeEdit(Integer coachId){
        ModelAndView modelAndView = new ModelAndView("coach/coach-course-fee-edit");
        if (coachId != null) {
            modelAndView.addObject("coach", coachService.queryCoachById(coachId));
            modelAndView.addObject("coachFee", coachCourseFeeService.queryByCoachId(coachId));
            modelAndView.addObject("basics" ,JsonUtils.obj2Json(coachCourseBasicFeeService.queryCoachCourseBasicsByCoachId(coachId)));
        }

        modelAndView.addObject("courseStandards", JsonUtils.obj2Json(courseStandardService.queryAllCourseStandardIdAndName()));

        return modelAndView;
    }

    /**
     * 保存教练课时费
     *
     * @param coachCourseFee 课时费
     * @return true 保存成功 / false 保存失败
     */
    @RequestMapping("/save")
    @ResponseBody
    public String save(CoachCourseFee coachCourseFee){

        boolean  suc = false;
        try {
            suc = coachCourseFeeService.save(coachCourseFee);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("保存教练课时费出错====> " + e.toString() + e.getCause());
        }
        if (suc) {
            return "true";
        }
        return "false";
    }

    /**
     * 教练补充课时费/扣费编辑
     *
     * @param coachId 教练 ID
     * @param year 年
     * @param month 月
     * @return 教练课时费/扣费编辑页面
     */
    @RequestMapping("/coach_course_adjust_fee")
    public ModelAndView coachCourseAdjustFeeEdit(@RequestParam Integer coachId, @RequestParam Integer year, @RequestParam Integer month){
        ModelAndView modelAndView = new ModelAndView("coach/coach-course-fee-adjust");
        if (coachId != null) {
            modelAndView.addObject("coach", coachService.queryCoachById(coachId));
            modelAndView.addObject("year", year);
            modelAndView.addObject("month", month);
            modelAndView.addObject("coachAdjust", coachCourseAdjustFeeService.queryByCoachId(coachId, year, month));
            modelAndView.addObject("basics" ,JsonUtils.obj2Json(coachCourseBasicFeeService.queryCoachCourseBasicsByCoachId(coachId)));
        }
        modelAndView.addObject("courseStandards", JsonUtils.obj2Json(courseStandardService.queryAllCourseStandardIdAndName()));

        return modelAndView;
    }



    /**
     * 保存教练补充课时费/扣费编辑
     *
     * @param coachCourseAdjustFee 补充课时/扣费
     * @return true 保存成功 / false 保存失败
     */
    @RequestMapping("/save_coach_course_adjust_fee")
    @ResponseBody
    public String save(CoachCourseAdjustFee coachCourseAdjustFee){

        boolean  suc = false;
        try {
            suc = coachCourseAdjustFeeService.save(coachCourseAdjustFee);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("保存教练课时费出错====> " + e.toString() + e.getCause());
        }
        if (suc) {
            return "true";
        }
        return "false";
    }



}